"""
Copyright (c) 2008, Luke Freeman
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:
	
	* Redistributions of source code must retain the above copyright notice, this 
	  list of conditions and the following disclaimer.
	* Redistributions in binary form must reproduce the above copyright notice, this 
	  list of conditions and the following disclaimer in the documentation and/or 
	  other materials provided with the distribution.
	* Neither the name of Flag-Up nor the names of its contributors may 
	  be used to endorse or promote products derived from this software without specific 
	  prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS 
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
"""

import pygtk
import gtk, gtk.glade
import gobject

from config.account_manager import *
from config.configuration import *


class AccountDialog:

	__config = Configuration()          # application configuration item
	__acct_mgr = AccountManager()       # the account manager

	# class initializer
	def __init__(self, parent, account):

		pref_glade_file = 'ui/glade/account_dialog.glade'
		self.widgets = gtk.glade.XML(pref_glade_file)
		self.window = self.widgets.get_widget('dialog_add_account')
		self.window.set_transient_for(parent);

		self.widgets.signal_autoconnect(self)         # connect the signals defined in the glade file


		self.account = account 		# the account details (None if in add mode)

		# get list of widgets
		self.cbo_account_type = self.widgets.get_widget("cbo_account_type")
		self.txt_account_name = self.widgets.get_widget("txt_account_name")
		self.txt_user_name = self.widgets.get_widget("txt_user_name")
		self.txt_password = self.widgets.get_widget("txt_password")
		self.chk_account_enabled = self.widgets.get_widget("chk_account_enabled")


		# populate the account type list dynamically from the found snap-ins
		acc_type_list = gtk.ListStore(gobject.TYPE_STRING)
		for snapin in self.__config.all_snapins():
			acc_type_list.append([snapin.type_description])

		self.cbo_account_type.set_model(acc_type_list)

		cell_render = gtk.CellRendererText()
		self.cbo_account_type.pack_start(cell_render, True)
		self.cbo_account_type.add_attribute(cell_render, 'text', 0) 

		self.cbo_account_type.set_active(0)		# select first element



	# helper to get currently selected account type
	def get_selected_account_type(self):
		model = self.cbo_account_type.get_model()
		act_iter = self.cbo_account_type.get_active_iter()
		sel_acct_type = model.get_value(act_iter, 0)

		return sel_acct_type


	# the main loop for the dialog
	def show(self):

		new_account = None
		result = None

		# if edit mode, show values        
		if self.account is not None:
			self.txt_account_name.set_text(self.account.name)


		# block a response until all of the information has been filled out correctly
		# or the user presses cancel
		while (new_account is None and result != gtk.RESPONSE_CANCEL):

			result = self.window.run()
			if (result == gtk.RESPONSE_OK):

				a_type = self.get_selected_account_type()
				a_name = self.txt_account_name.get_text()
				a_user = self.txt_user_name.get_text()
				a_pass = self.txt_password.get_text()
				a_enabled = self.chk_account_enabled.get_active()


				access, message = self.__acct_mgr.can_create(a_name, a_type, a_user, a_pass)
				if access:
					new_account = Account(a_name, a_type, a_user, a_pass, a_enabled)
					if self.account is not None:
						new_account.aid = self.account.aid		# if updating

				# cannot create the account
				else:
					message_box = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, gtk.BUTTONS_NONE, 
						"Invalid Entries")
					message_box.format_secondary_text(message)
					message_box.add_button(gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE)
					response = message_box.run()
					if response == gtk.RESPONSE_CLOSE:
						message_box.destroy()

		self.window.destroy()

		return result, new_account



	# event handlers ----
	# dialog buttons
	def on_btn_type_info_clicked(self, widget):
		a_type = self.get_selected_account_type()
		snapin = self.__config.snapin_by_type_description(a_type)

